<template>
  <div>
    <el-card class="box-construction" shadow="always">
      <div class="box-title box-title-position">
        <div>用户管理</div>
        <el-button class="add-btn" size="medium" type="primary" @click="addUser()">新增</el-button>
      </div>
      <div class="searchDiv">
        <el-row :gutter="20">
            <el-col :span="6"><el-input v-model="search.blurry" placeholder="请输入内容" size="small" clearable></el-input></el-col>
            <el-col :span="2"> <el-button type="primary" size="small" icon="el-icon-search" @click="getList(1)">搜索</el-button></el-col>
        </el-row>
    </div>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        style="width: 100%;margin-top:25px"
      >
        <el-table-column :show-overflow-tooltip="true" prop="id" label="ID" width="80"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="account" label="账户名"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="realname" label="真实姓名"></el-table-column>
        <el-table-column prop="version" label="操作" width="200">
          <template slot-scope="scope" class="operation">
            <el-button size="mini" icon="el-icon-edit"  @click="editUser(scope.row)">编辑</el-button>
            <el-button size="mini" icon="el-icon-delete" type="danger" @click="delUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="box-page"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pagination.currentPage"
        :page-sizes="[5,10, 20, 30, 40]"
        :page-size="pagination.pageSize"
        layout="total,sizes, prev, pager, next"
        :total="pagination.total"
      ></el-pagination>
    </el-card>
    <EditForm
      ref="editForm"
      :title="formTitle"
      :visible.sync="visible"
      :baseStyle="baseStyle"
      @saveForm="submit"
    />
  </div>
</template>

<script>
import stageAPI from "@apis/projectStage";
import orgAPI from "@apis/organize";
import EditForm from "./editForm/index";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      editRow: {},
      visible: false,
      baseStyle: {
        width: "100px",
      },
      formTitle: "新增用户",
      search: {
        blurry: ''
      },
      pagination: {
        currentPage: 1,
        pageNo: 1,
        pageSize: 5,
        total: 0,
      },
    };
  },
  components: {
    EditForm,
  },
  created() {
    this.loading = true;
    this.getList();
  },
  mounted() {},
  methods: {
    getList(val = 1) {
      let parmars = {
        pageNo: val,
        pageSize: this.pagination.pageSize,
        blurry: this.search.blurry
      };
      if(val===1){
        this.pagination.currentPage = 1
      }
      orgAPI
        .userList(parmars)
        .then((res) => {
          // this.pagination = {
          //   currentPage: res.current_page,
          //   pageNo: res.current_page,
          //   pageSize: res.per_page,
          //   total: res.total,
          // };
          this.pagination.total = res.total
          this.tableData = [...res.rows];
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(err.message);
        });
    },
    //控制每页条数
    handleSizeChange(val) {
      // console.log(`每页${val}条`);
      this.pagination.pageSize = val;
      this.loading = true;
      this.getList(this.pagination.pageNo);
    },
    //分页
    handleCurrentChange(val) {
      // console.log(`当前第${val}页`);
      this.loading = true;
      this.getList(val);
    },
    addUser() {
      this.formTitle = "新增用户";
      this.visible = true;
      this.editRow = {};
      this.$refs.editForm.setData(this.editRow)
    },
    editUser(data){
      this.formTitle = "编辑用户"
      this.visible = true
      // data.role = [3]
      this.editRow = JSON.parse(JSON.stringify(data))
      this.$refs.editForm.setData(this.editRow)
    },
    delUser(data) {
      this.$confirm("删除用户, 是否继续?", "提示", {
        closeOnClickModal: false,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.pagination.pageNo > 1 && this.tableData.length == 1) {
            this.pagination.pageNo = this.pagination.pageNo - 1;
          }
          this.loading = true;
          orgAPI
            .delUser(data.id)
            .then((res) => {
              this.getList(this.pagination.pageNo);
            })
            .catch((err) => {
              this.$message.error(err.message);
            });
        })
        .catch(() => {});
    },
    submit(val) {
      let API = this.formTitle==='新增用户' ?  orgAPI.addUser:orgAPI.editUser
      // php接口参数 ：{
      //     role: "dev",
      //     dept: 1,
      //     ...val,
      //   }
      API(val)
      .then((res) => {
        this.$refs.editForm.cancel()
        this.loading = true
        if(this.formTitle==='新增用户'){
          this.getList()
        }else{
          this.getList(this.pagination.currentPage)
        }
      })
      .catch((err) => {
        this.$refs.editForm.saveLoading = false
        this.$message.error(err.message)
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.box-title-position {
  position: relative;
  .add-btn {
    position: absolute;
    right: 0;
    top: -3px;
  }
}
.box-construction {
  min-height: 100px;
  .box-page {
    text-align: right;
    padding-top: 40px;
  }
  ::v-deep .searchDiv{
    margin: 10px 0px;
  }
}
// .operation{
//   .el-button {
//     // display: inline-block;
//   }
// }
</style>